package edu.java.lectures.lec12.threads.synchronization.dynamic.msgbox;

public class MsgPublisher extends Thread {

    private int counter = 1;
    private MsgBox msgBox;

    public MsgPublisher(String name, MsgBox msgBox) {
	super(name);
	this.msgBox = msgBox;
    }

    public MsgPublisher(String name, Runnable runnableObj, MsgBox msgBox) {
	super(runnableObj, name);
	this.msgBox = msgBox;
    }

    @Override
    public void run() {
	while (true) {
	    // try {
	    // Thread.sleep(500l);
	    // } catch (InterruptedException e) {
	    // e.printStackTrace();
	    // }
	    try {
		msgBox.putMessage("Publisher " + getName()
			+ " has just published msg: " + counter);
	    } catch (InterruptedException e) {
		e.printStackTrace();
		return;
	    }
	    System.out.println("Publisher " + getName()
		    + " has just published msg: " + counter);
	    counter++;
	}
    }
}
